﻿using System;
using System.Text;
using System.Threading;
using System.Timers; 
using System.Collections.Specialized;
using System.Threading.Tasks;
using Quartz.Impl;
using Quartz;
using Quartz.Logging;
using Quartz.Impl.Matchers;
using static Quartz.MisfireInstruction; 
  
namespace ParkingDataSimulate
{
    public partial class ScheduleParentsXXXXXX
    { 
        public static int IntervalHours { get; set; }
        public static string NumberPlate { get; set; }
        public static async Task RunSecondlyGlobalProgram(int intervalHours)
        {
            IntervalHours = intervalHours;
            DateTime startTime = DateTime.Now;

            try
            {
                NameValueCollection props = new NameValueCollection
                {
                    { "quartz.serializer.type", "binary" }
                };
                StdSchedulerFactory factory = new StdSchedulerFactory(props);
                IScheduler scheduler = await factory.GetScheduler();

                await scheduler.Start();

                IJobDetail job = JobBuilder.Create<ScheduleParentsJob>()
                    .WithIdentity("JOB1_Parents", "GROUP_Parents")
                    .Build();
                ITrigger trigger = TriggerBuilder.Create()
                    .WithIdentity("TRIGGER1_Parents")
                    .StartAt(startTime)
                    .WithCalendarIntervalSchedule(c=>c.WithInterval(IntervalHours, IntervalUnit.Hour))
                    .ForJob("JOB1_Parents", "GROUP_Parents")  
                    .Build();
                 string loggerLineMonthly = $"[FUNC::ParkingDataSimulateTask.RunSecondlyGlobalProgram] [RUN SCHEDULE PARENTS]";
                 Console.WriteLine(loggerLineMonthly);

                await Task.Delay(TimeSpan.FromMinutes(2));

                await scheduler.ScheduleJob(job, trigger);
                 
                await Task.Delay(TimeSpan.FromMinutes(startTime.Minute*2));
                
            }
            catch (SchedulerException se)
            {
                string loggerLineMonthly = $"[EXCEPTION::SchedulerException][FUNC::ScheduleParents.RunSecondlyGlobalProgram][{se.Message}][{se.StackTrace}]";
                await Console.Error.WriteLineAsync(loggerLineMonthly);
            }
        }

        public class ScheduleParentsJob : IJob
        { 
            public async Task Execute(IJobExecutionContext context)
            {

                int IntervalSeconds = 20;
                ParkingDataSimulateTask.Stop = false;
                ParkingDataSimulateTask.IntervalSeconds = IntervalSeconds; ;

#if DEBUG

                Console.WriteLine("INPUT NUMBER PLATE TO CONTINUE,INPUT 1 TO SKIP.....");

                string numberPlate1 = Console.ReadLine();

                while (true)
                {
                    if (!string.IsNullOrEmpty(numberPlate1))
                        break;

                    Console.WriteLine("INPUT NUMBER PLATE TO CONTINUE,INPUT 1 TO SKIP.....");

                    numberPlate1 = Console.ReadLine();
                }

                if (numberPlate1.Equals("1"))
                {
                    numberPlate1 = string.Empty;
                }

                ParkingDataSimulateTask.NumberPlate = numberPlate1;
#endif

                await ParkingDataSimulateTask.RunSecondlyGlobalProgram(IntervalSeconds);
                 

                await Console.Out.WriteLineAsync(string.Format("\n[{0:yyyy-MM-dd HH:mm:ss fff}] [INFO] [FUNC::ScheduleSecondlyGlobalJob.Execute] [{1}]", DateTime.Now, context.FireInstanceId));
  
            }
        }
    }
}